<template>
  <div>
    <el-card class="box-card">
      <el-row>
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">系统管理</el-breadcrumb-item>
          <el-breadcrumb-item>仓库管理</el-breadcrumb-item>
        </el-breadcrumb>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-form  :model="dto" label-width="100px" class="form-inline" size="small" ref="dtoForm" :rules="dtoRules">
          <el-col :span="6">
            <el-form-item label="仓库名称" prop="name">
              <el-input v-model="dto.name" placeholder="请输入仓库名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="仓库地址" prop="address">
              <el-input v-model="dto.address" placeholder="仓库地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="管理员" :label-width="formLabelWidth" prop="employeeid" >
              <el-select v-model="dto.employeeid" filterable placeholder="请选择管理员" style="width: 100%;">
                <el-option
                  v-for="item in employeeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="仓库状态" prop="status">
              <el-select v-model="dto.status" clearable placeholder="请选择">
                <el-option label="启用" value="1"></el-option>
                <el-option label="停用" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-button type="primary" icon="el-icon-search" @click="getWarehouseList()">查询</el-button>
          <el-button type="warning" @click="resetForm('dtoForm')">清空</el-button>
          <el-button type="success" round @click="HandleAdd()">添加仓库</el-button>
        </el-col>
      </el-row>
      <el-row><br><br></el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="22">
          <el-table
            :data="warehouseList"
            style="width: 100%">
            <el-table-column
              label="序号"
              type="index"
              width="50">
            </el-table-column>
            <el-table-column
              prop="name"
              label="仓库名称"
              width="120">
            </el-table-column>
            <el-table-column
              prop="address"
              label="地址"
              width="200">
            </el-table-column>
            <el-table-column
              label="状态"
              width="80">
              <template slot-scope="scope">
                <span style="color: green;" v-if="scope.row.status == 1">可使用</span>
                <span style="color: red;" v-else>已停用</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="updatetime"
              label="更新时间"
              width="100">
            </el-table-column>
            <el-table-column
              prop="admin.name"
              label="管理员"
              width="150">
            </el-table-column>
            <el-table-column label="操作" width="300" fixed="right">
              <template slot-scope="scope">
                <el-button
                  size="mini" type="primary" icon="el-icon-edit"
                  @click="handleEdit(scope.row)">编辑</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 1" type="warning"
                  @click="handleDisable(scope.row)">停用</el-button>
                <el-button
                  size="mini" v-if="scope.row.status == 0" type="success"
                  @click="handleEnable(scope.row)">启用</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="1"><br></el-col>
        <el-col :span="5">
          <el-pagination
            @current-change="handleCurrentChange"
            :current-page.sync="dto.page"
            :page-size="5"
            layout="total, prev, pager, next"
            :total="total">
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <!-- 表单弹出框 -->
    <el-dialog :title="formtitle" :visible.sync="dialogWarehouseVisible" width="30%" @close="close('ruleForm')">
      <el-form :model="warehouse" :rules="rules" ref="ruleForm">
        <el-form-item label="仓库名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="warehouse.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="仓库地址" :label-width="formLabelWidth" prop="address">
          <el-input v-model="warehouse.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="仓库管理员" :label-width="formLabelWidth" prop="employeeid" >
          <el-select v-model="warehouse.employeeid" filterable placeholder="请选择管理员" style="width: 100%;">
            <el-option
              v-for="item in employeeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="handleConfirm('ruleForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data(){
    var validateAtLeastOneChange = (rule, value, callback) => {
      if (this.warehouse.name == this.oldwarehouse.name && this.warehouse.address == this.oldwarehouse.address && this.warehouse.employeeid == this.oldwarehouse.employeeid) {
        callback(new Error('请至少修改一项数据'));
      } else{
        callback();
      }
    }
    return{
      dto:{
        name:"",
        address:"",
        status:"",
        employeeid:"",
        page:1,
        size:5
      },
      total:"",
      employeeList:[],
      warehouseList:[],
      warehouse:{
        id:"",
        name:"",
        address:"",
        employeeid:""
      },
      oldwarehouse:{
        id:"",
        name:"",
        address:"",
        employeeid:""
      },
      formtitle:"",
      dialogWarehouseVisible:false,
      formLabelWidth:"100px",
      rules: {
        name: [
          { required: true, message: '请输入客户名称', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ],
        employeeid: [
          { required: true, message: '请选择管理员', trigger: 'change' },
          { validator: validateAtLeastOneChange, trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
          { validator: validateAtLeastOneChange, trigger: 'blur' }
        ]
      }
    }
  },
  mounted(){
    this.getEmployeeList()
    this.getWarehouseList()
  },
  methods:{
    handleCurrentChange(val){
      this.dto.page = val
      this.getWarehouseList()
    },
    getEmployeeList(){
      this.$axios.get('/warehouse/getWarehouseAdminList').then(res=>{
        if(res.data.code == 200){
          this.employeeList = res.data.data
        }
      })
    },
    getWarehouseList(){
      this.$axios.get('/warehouse/getWarehouseList',{
        params:this.dto
      }).then(res=>{
        if(res.data.code == 200){
          this.warehouseList = res.data.data.data
          this.total = res.data.data.total
        }
      })
    },
    //重置查询表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    close(formName){
      this.$refs[formName].clearValidate();
      this.dialogWarehouseVisible = false
    },
    HandleAdd(){
      this.warehouse = {
        id:"",
        name:"",
        address:"",
        employeeid:""
      }
      this.dialogWarehouseVisible = true
      this.formtitle = "添加仓库"
    },
    handleEdit(row){
      this.warehouse = {
        id:row.id,
        name:row.name,
        address:row.address,
        employeeid:row.employeeid
      }
      this.oldwarehouse = row
      this.dialogWarehouseVisible = true
      this.formtitle = "编辑仓库"
    },
    handleConfirm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formtitle == '添加仓库') {
            this.$axios.post('/warehouse/addWarehouse',this.warehouse).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '添加成功！',
                  type: 'success'
                });
                this.dialogWarehouseVisible = false
                this.getWarehouseList()
              }
            })
          }
          if (this.formtitle == '编辑仓库') {
            this.$axios.put('/warehouse/editWarehouse',this.warehouse).then(res=>{
              if (res.data.code == 200) {
                this.$message({
                  message: '编辑成功！',
                  type: 'success'
                });
                this.dialogWarehouseVisible = false
                this.getWarehouseList()
              }
            })
          }     
        } else {
          return false;
        }
      }); 
    },
    handleEnable(row){
      this.$axios.put('/warehouse/enableWarehouse/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            message: '启用成功！',
            type: 'success'
          });
          this.getWarehouseList()
        }
      })
    },
    handleDisable(row){
      this.$axios.put('/warehouse/disableWarehouse/' + row.id).then(res=>{
        if (res.data.code == 200) {
          this.$message({
            message: '禁用成功！',
            type: 'success'
          });
          this.getWarehouseList()
        }
      })
    },
    handleDelete(row){
      this.$confirm('此操作将永久删除该客户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete('/warehouse/deleteWarehouse/' + row.id).then(res=>{
          if (res.data.code == 200) {
          this.$message({
            message: '删除成功！',
            type: 'success'
          });
          this.getWarehouseList()
        }
      })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });          
      });
    }
  }
}
</script>
<style scoped>
.box-card {
  height: 79vh;
}
.el-form-item {
  text-align: left;
  width: 90%;
}
</style>
